//实际引用:利用栈解决括号匹配问题--编译器
import java.util.Stack;

public class Solution {

    public boolean isValid(String s){
        Stack<Character> stack =new Stack<Character>();

        for (int i =0; i<s.length(); i++){
            char c =s.charAt(i);
            if(c=='(' || c=='{' || c=='['){
                stack.push(c);
            }else{
                if(stack.isEmpty())
                    return  false ;
                char topchar =stack.pop();
                if (c==')' && topchar != '(' )
                    return false;
                if (c==']' && topchar != '[' )
                    return false;
                if (c=='}' && topchar != '{' )
                    return false;
            }
        }
        //循环完成代表匹配成功，但如果栈内还有元素也不算匹配成功，所以
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println((new Solution()).isValid("()[]{}"));
        System.out.println((new Solution()).isValid("([)]"));
    }

}
